import {h} from 'vue'
import {RouterLink} from 'vue-router'
import {MenuObjPropNameEnum} from "../enums/menu-obj-prop-name-enum.ts"
import {MenuTypeEnum} from "../enums/menu-type-enum.ts"
import MenuIcon from "../components/MenuIcon.vue"

/**
 * 渲染菜单图标的处理函数
 *
 * @param option 菜单项配置
 */
export const renderMenuIconHandler = (option) => {
  return h(
    MenuIcon,
    {
      icon: option[MenuObjPropNameEnum.ICON],
      iconType: option[MenuObjPropNameEnum.ICON_TYPE],
      isFlexCenter: true
    }
  )
}

/**
 * 渲染菜单标签的处理函数
 *
 * @param option 菜单项配置
 */
export const renderMenuLabelHandler = (option) => {
  // 当前菜单项被禁用
  if (option[MenuObjPropNameEnum.DISABLED]) return option.label
  // 当前菜单项类型为菜单
  if (option[MenuObjPropNameEnum.TYPE] === MenuTypeEnum.MENU) {
    return h(
      RouterLink,
      {
        to: {
          path: option[MenuObjPropNameEnum.ROUTE_PATH],
        }
      },
      {
        default: () => option.label
      }
    )
  }
  // 当前菜单项类型为外链
  else if (option[MenuObjPropNameEnum.TYPE] === MenuTypeEnum.LINK) {
    return h(
      'a',
      {
        href: option[MenuObjPropNameEnum.LINK_PATH],
        target: '_blank'
      },
      {
        default: () => option.label
      }
    )
  } else {
    return option.label
  }
}
